package com.woniu.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.dao.UserMapper;
import com.woniu.dao.WorkerMapper;
import com.woniu.entity.User;
import com.woniu.entity.UserExample;
import com.woniu.entity.Worker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询员工权限并封装
 */
@Component("workerDetailsServiceImpl")
public class WorkerDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private WorkerMapper workerMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ObjectMapper obj = new ObjectMapper();

        Worker worker = workerMapper.getWorkerByAccount(account);

        if (StringUtils.isEmpty(worker)){
            //表示没有该用户
            return null;
        }
        String keys =worker.getAccount()+worker.getPassword();

        //查权限,先从缓存中去找
        String values = ops.get(keys);
        if (!StringUtils.isEmpty(values)){
            try {
                List<GrantedAuthority> authorities = new ArrayList<>();
                //从缓存中查到了他的所有权限
                List<String> pro = obj.readValue(values, new TypeReference<List<String>>() {
                });

                pro.forEach(per->{
                    authorities.add(new SimpleGrantedAuthority(per));
                });
                return new org.springframework.security.core.userdetails.User(worker.getAccount(),worker.getPassword(),authorities);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }

        //从数据库中去查
        //创建存放权限的集合 --暂时为空集合
        List<GrantedAuthority> authorities = new ArrayList<>();
        List<String> perCodes = workerMapper.selectPercodeByPerm(worker.getAccount());
        perCodes.forEach(per->{
            authorities.add(new SimpleGrantedAuthority(per));
        });
        //根据数据库查询信息，封装UserDetails对象，并返回
        UserDetails userDetails = new org.springframework.security.core.userdetails.User(worker.getAccount(),worker.getPassword(),authorities);
        return userDetails;
    }
}
